<template>
  <el-container class="home_box">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button @click="logout()">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="menuIsCollapse ? '64px' : '200px'">
        <div class="menu_collapse_toggle" @click="toggleMenuCollapse()">
          |||
        </div>
        <el-menu
          :default-active="$route.path.slice(1)"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
          :collapse="menuIsCollapse"
          :collapse-transition="false"
          :router="true"
        >
          <el-submenu v-for="v in menus" :key="v.id" :index="v.path">
            <template slot="title">
              <i class="el-icon-help"></i>
              <span v-text="v.authName"></span>
            </template>
            <el-menu-item v-for="v1 in v.children" :key="v1.id" :index="v1.path">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span v-text="v1.authName"></span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      menuIsCollapse: false,
      menus: []
    }
  },
  methods: {
    toggleMenuCollapse() {
      this.menuIsCollapse = !this.menuIsCollapse
    },
    logout() {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    }
  },
  async created() {
    const { data: res } = await this.$http.get('/menus')
    console.log(res, 'res')
    if (res.meta.status !== 200) {
      return this.$message.error('获取左侧菜单数据失败')
    }
    this.menus = res.data
  }
}
</script>

<style lang="less" scoped>
.home_box {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
    > span {
      margin-left: 15px;
      font-size: 20px;
      color: #fff;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
  .menu_collapse_toggle {
    background-color: #4a5064;
    height: 25px;
    line-height: 25px;
    text-align: center;
    color: #fff;
    letter-spacing: 2px;
    cursor: pointer;
  }
}

.el-main {
  background-color: #eaedf1;
}
</style>
